/**
 * @(#) AddressProxy.as
 *
 * @date		2009. 02. 18.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	 주소변경 - Proxy
 * 				
 */
package com.skt.common_hr.myprofile_stc.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.myprofile_stc.model.vo.ModLicenseVO;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class LicenseProxy extends CommonProxy
	{
		public static const NAME:String = "LicenseProxy";
		
		public static const LOAD_LICENSE_COMPLETE:String 	= "loadLicenseComplete";
		public static const LOAD_LICENSE_FAILED:String 		= "loadLicenseFailed";
		public static const SAVE_LICENSE_COMPLETE:String 	= "saveLicenseComplete";
		public static const SAVE_LICENSE_FAILED:String 		= "saveLicenseFailed";
		
		public var licenseList:ArrayCollection;
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function LicenseProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		
		
		public function load(pernr:String):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_ACTIO"] = "DIS";
				
			loadData("Z_HR_0123",params, result, fault); 
		}
		
		/**
		 * 주소를 저장함
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param   pernr: 사번
		 * 			PSTLZ: 우편번호
		 * 			LOCAT: 상세주소
		 * 			SUBTY: 주소 구분 (1-본적, 2-현주소, 3-주민등록주소)
		 * @return void
		 */
		public function save(pernr:String, table:Array):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_ACTIO"] = "INS";
			
			var tables:Object = new Object();
			tables["T_IN"] = table;
			
			saveData("Z_HR_0123", params, tables, resultSave, faultSave); 
		}



		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {
				licenseList = parseLicenseList(event.result.T_OUT);
				facade.sendNotification(LOAD_LICENSE_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_LICENSE_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_LICENSE_FAILED, this);
		}
		
		
		public function resultSave(event:ResultEvent):void
		{
			if(retCode==0) {
				facade.sendNotification(SAVE_LICENSE_COMPLETE, this);
			}
			else {
				facade.sendNotification(SAVE_LICENSE_FAILED, this);
			}
		}
		
		private function faultSave(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_LICENSE_FAILED, this);
		}
		
		
		private function parseLicenseList(src:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection();
			for each (var item:Object in src)
			{
				var vo:ModLicenseVO = new ModLicenseVO;
				
				vo.BEGDA	=	DateUtil.stringToDateTime(item.BEGDA);	//발령 유형 이름
				vo.ENDDA	=	DateUtil.stringToDateTime(item.ENDDA);	//발령 사유 이름
				vo.ZNAME 	=   item.ZNAME;							//자격증 명칭
				vo.ZGRADE 	=   item.ZGRADE;						//자격증 등급
				vo.ZLCNUMB 	=   item.ZLCNUMB;						//자격증 번호
				vo.ZCOMPAN 	=   item.ZCOMPAN;						//인증기관
				vo.ZGETDAT 	=   DateUtil.stringToDateTime(item.ZGETDAT);			// 취득일	
				vo.ZCOMMENT =   item.ZCOMMENT;							//비고
			
				list.addItem(vo);
			}
			return list;
		}
		
		public function toJSONobject(vo:ModLicenseVO):Object
		{
			var jsonObj:Object = new Object();
				
				jsonObj["BEGDA"]	=	DateUtil.toBasicString(vo.BEGDA);	//발령 유형 이름
				jsonObj["ENDDA"]	=	DateUtil.toBasicString(vo.ENDDA);	//발령 사유 이름
				jsonObj["ZNAME"]	= 	vo.ZNAME;	//부서명(상위조직명 포함)
				jsonObj["ZGRADE"]	=	vo.ZGRADE;		//최종직위
				jsonObj["ZLCNUMB"]	=	vo.ZLCNUMB;		//근무지
				jsonObj["ZCOMPAN"]	=	vo.ZCOMPAN;		//최종담당업무
				jsonObj["ZGETDAT"]	=	DateUtil.toBasicString(vo.ZGETDAT);		//경력인정기간(연)
				jsonObj["ZCOMMENT"]	=	vo.ZCOMMENT;		//경력인정기간(월)

			return jsonObj;
		}
	}
}